import fse, { type WriteStream } from "fs-extra";

/**
 * 创建一个管道流，将文件输送到可写流writeStream
 * @param filePath 文件路径
 * @param writeStream 可写流
 * @returns Promise
 */
export const pipeStream = (filePath: string, writeStream: WriteStream) =>
    new Promise(resolve => {
        // 创建一个可读流，用于读取文件
        const readStream = fse.createReadStream(filePath);

        // 将可读流和可写流连接起来
        readStream.pipe(writeStream);

        // 当文件读取完毕时，删除文件，并resolve
        readStream.on("end", () => {
            fse.unlinkSync(filePath);
            resolve("");
        });
    });
